<!--
   用于上课type,typeId参数必传
-->
<template>
	<chat 
		v-if="isShowChat"
		type="class"
		:typeId="classId"
		ref="chatRoom" 
		:userList="userList" 
		:groupList="groupList" 
		:groupType="2"
		@filterUsers="getUserList">
	</chat>
</template>

<script>
	import {
		getGroupUsers,
		getGroupInfo,
		addGroupUsers,
		delGroupUsers,
	} from '@/api/boardcast_new.js'
	import {queryClassInfo,getClassUsers} from '@/api/selfStudy/class_new.js'
	import {
		getExperGroupsUsers
	} from '@/api/users.js'
	import {
		getDictInfoByName
	} from '@/api/dict.js'
	import encryption from '@/libs/encryption.js'
	import chat from "./components/chat.vue"
	export default {
		name: 'chatRoom',
		props: {
			'classId':{
				type: String,
	      		required: true
			},
			'teacherId':{
				type: String,
	      		required: true
			},
			'teacherName':{
				type: String,
	      		required: true
			}
		},
		components: {
			chat
		},
		data() {
			let November = sessionStorage.getItem('November')
			November = JSON.parse(encryption.SelfUseDecrypt(November))
			return {
				USER_ID: November.userId,
				//      USER_Name:October.nickname,
				//      avatar:October.avatar,
				userList: [],
				userListbf: [],
				groupList: [],
				notReadChat: [],
				queryCompanyUserInfo: {
					pageIndex: 1,
					pageSize: 2147483646, //2147483646
					groupId: ''
				},
				totalCount: 0,
				isShowChat: 0,
				expGroupUserList: [],
				chatGroupUserList: [],
			}
		},
		watch: {

		},
		filters: {

		},
		computed: {
			
		},
		mounted() {
			if(!this.classId) {
				return;
			}
			this.init();
		},
		beforeDestroy() {

		},
		methods: {
			init() {
				this.queryCompanyUserInfo.groupId = this.classId;
				this.getGroupList();
				this.getDictInfoByName();
				//获取所有聊天用户
				this.getChatGroupList();
			},
			getUserList(page, userName) {
				if(this.userListbf.length > 0) {
					if(userName) {
						this.userList = this.userListbf.filter(user => user.userName.indexOf(userName) != -1);
					} else {
						this.userList = this.userListbf;
					}
					return
				}
				getGroupUsers(this.queryCompanyUserInfo).then(data => {
					if(data.code == 1) {
						this.userList = data.data.items.filter(user => user.userId != this.teacherId) || [];
						this.userListbf = data.data.items.filter(user => user.userId != this.teacherId) || [];
						this.userList.forEach((user) => {
							this.$set(user, 'msgList', []); //当前显示消息
							this.$set(user, 'msgCount', 0); //当前未读消息数
						});
					} else {
						this.$Message.error(data.message);
					}
				}).catch(error => {
					console.log(error)
				})
			},
			//获取班级详情
			getGroupList() {
				queryClassInfo(this.queryCompanyUserInfo.groupId).then(data => {
					if(data.code == 1) {
						this.classInfo = data.data;
						this.groupList = [{
							id: this.classInfo.id,
							name: this.classInfo.egName
							}
						];
						this.groupList.forEach((group) => {
							this.$set(group, 'msgList', []); //当前显示消息
							this.$set(group, 'msgCount', 0); //当前未读消息数
						});
						this.getUserList(1);
						// this.classInfo = data.attribute.data;
						// this.groupList = [{
						// 	id: data.attribute.data.id,
						// 	name: data.attribute.data.className
						// }];
						// this.groupList.forEach((group) => {
						// 	this.$set(group, 'msgList', []); //当前显示消息
						// 	this.$set(group, 'msgCount', 0); //当前未读消息数
						// });
						// Promise.all([this.getExperGroupsUsers(), this.getChatGroupList()]).then(() => {
						// 	let delUsers = this.chatGroupUserList.filter(user => !this.expGroupUserList.map(user => user.userId).includes(user.userId));
						// 	let addUsers = this.expGroupUserList.filter(user => !this.chatGroupUserList.map(user => user.userId).includes(user.userId));
						// 	this.syncChatGroupUsers(delUsers, addUsers).then(() => {
						// 		this.getUserList(1);
						// 	})
						// });
					} else {

					}
				})
			},
			//是否可以聊天
			getDictInfoByName() {
				getDictInfoByName({
					name: 'allowCommunicate'
				}).then(data => {
					if(data.code == 1) {
						this.isShowChat = data.attribute.data.value && this.teacherId == this.USER_ID && this.teacherName && this.classId;
					} else {

					}
				})
			},
			//获取班级实验组成员
			getExperGroupsUsers() {
				return new Promise((resolve, reject) => {
					getExperGroupsUsers(this.classInfo.egId).then(data => {
						if(data.code == 1) {
							this.expGroupUserList = data.data;
							this.expGroupUserList.push({userId:this.teacherId,userName:this.teacherName});
							resolve();
						} else {
							this.$Message.error(data.message)
						}
					})
				})
			},
			//获取所有聊天用户
			getChatGroupList() {
				return new Promise((resolve, reject) => {
					let params = {
						pageIndex: 1,
						pageSize: 2147483646,
						groupId: this.classId
					}
					getGroupUsers(params).then(data => {
						if(data.code == 1) {
							this.chatGroupUserList = data.data.items;
							resolve();
						} else {
							this.$Message.error(data.message);
						}
					}).catch(error => {
						console.log(error)
					})
				})
			},
			//同步聊天人员数据
			async syncChatGroupUsers(delUsers, addUsers) {
				let groupId = this.groupList[0].id;
				if(addUsers.length > 0) {
					await addGroupUsers(groupId, addUsers.map(user => {
						return {
							userId: user.userId,
							userName: user.userName,
							avatar: '' //user.user.avatar
						}
					})).then(data => {
						if(data.code == 1) {

						} else {

						}
					}).catch(error => {

					})
				}
				if(delUsers.length > 0) {
					await delGroupUsers(groupId, delUsers.map(user => user.userId)).then(data => {
						if(data.code == 1) {

						} else {

						}
					}).catch(error => {

					})
				}
			}
		},
		destroyed() {

		}
	}
</script>

<style>

</style>